1
'*********************************** Module Header **************************************\
2 ' Module Name: MainForm.vb
3 ' Project: VBWinFormBindToNestedProperties
4 ' Copyright (c) Microsoft Corporation.
6 ' The sample demonstrates how to bind a DataGridView column to a nested property
9 ' This source is subject to the Microsoft Public License.
10 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 ' All other rights reserved.
13 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 '*****************************************************************************************/
18 Imports System
.ComponentModel
23 Private mylist
As New BindingList(Of Person
)
30 Private Sub Form1_Load(ByVal sender
As Object, ByVal e
As EventArgs
) Handles MyBase
.Load
31 ' bind the DataGridView to the list
32 Me.DataGridView1
.AutoGenerateColumns
= False
33 Me.DataGridView1
.DataSource
= mylist
35 Me.DataGridView1
.Columns
.Add("ID", "ID")
36 Me.DataGridView1
.Columns
.Add("Name", "Name")
37 Me.DataGridView1
.Columns
.Add("CityName", "City Name")
38 Me.DataGridView1
.Columns
.Add("PostCode", "Post Code")
40 CType(Me.DataGridView1
.Columns("ID"), DataGridViewTextBoxColumn
).DataPropertyName
= "ID"
41 CType(Me.DataGridView1
.Columns("Name"), DataGridViewTextBoxColumn
).DataPropertyName
= "Name"
42 CType(Me.DataGridView1
.Columns("CityName"), DataGridViewTextBoxColumn
).DataPropertyName
= "HomeAddr_CityName"
43 CType(Me.DataGridView1
.Columns("PostCode"), DataGridViewTextBoxColumn
).DataPropertyName
= "HomeAddr_PostCode"
46 Private Sub button1_Click(ByVal sender
As Object, ByVal e
As EventArgs
) Handles Button1
.Click
47 ' add objects of type Person to a list
52 Dim addr
As New Address
53 addr
.Cityname
= "city name1"
54 addr
.Postcode
= "post code1"
64 addr
.Cityname
= "city name2"
65 addr
.Postcode
= "post code2"